package org.jeecg.modules.purchase.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.purchase.entity.*;
import org.jeecg.modules.purchase.mapper.PurchaseStockInGoodsMapper;
import org.jeecg.modules.purchase.mapper.PurchaseStockInMapper;
import org.jeecg.modules.purchase.service.IPurchaseGoodsService;
import org.jeecg.modules.purchase.service.IPurchaseStockInService;
import org.jeecg.modules.purchase.service.IPurchaseStockService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 入库记录
 * @Author: jeecg-boot
 * @Date:   2022-11-16
 * @Version: V1.0
 */
@Service
public class PurchaseStockInServiceImpl extends ServiceImpl<PurchaseStockInMapper, PurchaseStockIn> implements IPurchaseStockInService {

	@Autowired
	private PurchaseStockInMapper purchaseStockInMapper;
	@Autowired
	private PurchaseStockInGoodsMapper purchaseStockInGoodsMapper;

	@Autowired
	private ISysBaseAPI sysBaseAPI;

	@Autowired
	private IPurchaseGoodsService purchaseGoodsService;

	@Resource
	private IPurchaseStockService purchaseStockService;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(PurchaseStockIn purchaseStockIn, List<PurchaseStockInGoods> purchaseStockInGoodsList) {
		purchaseStockInMapper.insert(purchaseStockIn);
		if(purchaseStockInGoodsList!=null && purchaseStockInGoodsList.size()>0) {
			for(PurchaseStockInGoods entity:purchaseStockInGoodsList) {
				//外键设置
				entity.setStockInId(purchaseStockIn.getId());
				entity.setStockInCode(purchaseStockIn.getCode());
				// 生成库存类
				PurchaseStock purchaseStock = createPurchaseStock(entity,purchaseStockIn.getRepositoryCode(),purchaseStockIn.getRepositoryName());
				purchaseStock.setTotalNumber(entity.getFinalNumber());
				// 入库需要对应增加库存中物品数量
				purchaseStockService.addStock(purchaseStock);
				purchaseStockInGoodsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(PurchaseStockIn purchaseStockIn,List<PurchaseStockInGoods> purchaseStockInGoodsList) {

		// 1.获取原入库单
		// 获取原入库单
		PurchaseStockIn oriPurchaseStockIn = this.getById(purchaseStockIn.getId());
		// 更新入库单
		this.updateById(purchaseStockIn);
		// 获取入库物品
		LambdaQueryWrapper<PurchaseStockInGoods> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(PurchaseStockInGoods::getStockInId,oriPurchaseStockIn.getId());
		List<PurchaseStockInGoods> oriPurchaseStockInGoodsList = purchaseStockInGoodsMapper.selectList(wrapper);

		// 2.根据新编辑的物品记录修改库存
		this.savePurchaseStockInGoods(purchaseStockIn,purchaseStockInGoodsList);

		// 3.撤销原入库单下的物品记录
		this.delPurchaseStockInGoods(oriPurchaseStockIn,oriPurchaseStockInGoodsList);

		// 4.添加新编辑的记录
		purchaseStockInGoodsList.forEach(entity->{
			purchaseStockInGoodsMapper.insert(entity);
		});

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		PurchaseStockIn purchaseStockIn = this.getById(id);
		// 撤销已经入库的库存
		LambdaQueryWrapper<PurchaseStockInGoods> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(PurchaseStockInGoods::getStockInId,id);
		// 获取入库单中对应的物品
		List<PurchaseStockInGoods> oriPurchaseStockInGoodsList = purchaseStockInGoodsMapper.selectList(wrapper);
		// 遍历修改库存
		oriPurchaseStockInGoodsList.forEach(entity->{
			PurchaseStock purchaseStock = createPurchaseStock(entity, purchaseStockIn.getRepositoryCode(), purchaseStockIn.getRepositoryName());
			purchaseStock.setTotalNumber(entity.getFinalNumber().negate());
			purchaseStockService.addStock(purchaseStock);
		});
		purchaseStockInGoodsMapper.deleteByMainId(id);
		purchaseStockInMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			PurchaseStockIn purchaseStockIn = this.getById(id);
			// 撤销已经入库的库存
			LambdaQueryWrapper<PurchaseStockInGoods> wrapper = new LambdaQueryWrapper<>();
			wrapper.eq(PurchaseStockInGoods::getStockInId,id);
			// 获取入库单中对应的物品
			List<PurchaseStockInGoods> oriPurchaseStockInGoodsList = purchaseStockInGoodsMapper.selectList(wrapper);
			// 遍历修改库存
			oriPurchaseStockInGoodsList.forEach(entity->{
				PurchaseStock purchaseStock = createPurchaseStock(entity, purchaseStockIn.getRepositoryCode(), purchaseStockIn.getRepositoryName());
				purchaseStock.setTotalNumber(entity.getFinalNumber().negate());
				purchaseStockService.addStock(purchaseStock);
			});
			purchaseStockInGoodsMapper.deleteByMainId(id.toString());
			purchaseStockInMapper.deleteById(id);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMainTemp(PurchaseStockIn purchaseStockIn, List<PurchaseStockInGoods> purchaseStockInGoodsList) {
		if(StringUtils.isBlank(purchaseStockIn.getCode())){
			String code = null;
			Result result = sysBaseAPI.executeByRuleCode("StockInCodeRule",new JSONObject());
			if(result.isSuccess()){
				code = (String)result.getResult();
			}
			purchaseStockIn.setCode(code);
		}
		purchaseStockInMapper.insert(purchaseStockIn);
		if(purchaseStockInGoodsList!=null && purchaseStockInGoodsList.size()>0) {
			for(PurchaseStockInGoods entity:purchaseStockInGoodsList) {
				//外键设置
				entity.setStockInId(purchaseStockIn.getId());
				purchaseStockInGoodsMapper.insert(entity);
			}
		}
	}
	// 撤销入库物品
	private void delPurchaseStockInGoods(PurchaseStockIn purchaseStockIn,List<PurchaseStockInGoods> oriPurchaseStockInGoodsList) {
		// 遍历修改库存
		oriPurchaseStockInGoodsList.forEach(entity->{
			PurchaseStock purchaseStock = createPurchaseStock(entity, purchaseStockIn.getRepositoryCode(), purchaseStockIn.getRepositoryName());
			purchaseStock.setTotalNumber(entity.getFinalNumber().negate());
			purchaseStockService.addStock(purchaseStock);
		});
		purchaseStockInGoodsMapper.deleteByMainId(purchaseStockIn.getId());
	}

	// 添加入库物品
	public void savePurchaseStockInGoods(PurchaseStockIn purchaseStockIn, List<PurchaseStockInGoods> purchaseStockInGoodsList) {
		if(purchaseStockInGoodsList!=null && purchaseStockInGoodsList.size()>0) {
			for(PurchaseStockInGoods entity:purchaseStockInGoodsList) {
				//外键设置
				entity.setStockInId(purchaseStockIn.getId());
				entity.setStockInCode(purchaseStockIn.getCode());
				// 生成库存类
				PurchaseStock purchaseStock = createPurchaseStock(entity,purchaseStockIn.getRepositoryCode(),purchaseStockIn.getRepositoryName());
				purchaseStock.setTotalNumber(entity.getFinalNumber());
				// 入库需要对应减少库存中物品数量
				purchaseStockService.addStock(purchaseStock);
//				purchaseStockInGoodsMapper.insert(entity);
			}
		}
	}

	private PurchaseStock createPurchaseStock(PurchaseStockInGoods entity, String repositoryCode, String repositoryName){
		PurchaseStock purchaseStock = new PurchaseStock();
		purchaseStock.setRepositoryName(repositoryName);
		purchaseStock.setRepositoryCode(repositoryCode);
		purchaseStock.setGoodsName(entity.getGoodsName());
		purchaseStock.setGoodsTypeCode(entity.getGoodsTypeCode());
		purchaseStock.setGoodsCode(entity.getGoodsCode());
		purchaseStock.setBrand(entity.getBrand());
		purchaseStock.setGoodsSize(entity.getSize());
		purchaseStock.setGoodsUnit(entity.getUnit());
		purchaseStock.setGoodsUnitPrice(entity.getUnitPrice());
		String goodsCode = entity.getGoodsCode();
		LambdaQueryWrapper<PurchaseGoods> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(PurchaseGoods::getCode,goodsCode);
		PurchaseGoods goods = purchaseGoodsService.getOne(queryWrapper);
		BigDecimal bigDecimal = new BigDecimal(Double.valueOf(goods.getSellingPrice()));
		purchaseStock.setSellingPrice(bigDecimal);
		return purchaseStock;
	}
	
}
